﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.service;
using DXApplicationTangche.DTO;

namespace DXApplicationTangche.UC.订单一览新.辅料.辅料管理
{
    public partial class Form辅料维护 : DevExpress.XtraEditors.XtraForm
    {
        private DTO辅料一览 dTO辅料一览 = new DTO辅料一览();
        private String subMaterialTypeId;
        private String subMaterialType;
        private Boolean is更新;
        public Form辅料维护(String subMaterialTypeId, String subMaterialType)
        {
            InitializeComponent();
            this.subMaterialTypeId = subMaterialTypeId;
            this.subMaterialType = subMaterialType;
            this.is更新 = false;
        }
        public Form辅料维护(DTO辅料一览 dTO)
        {
            InitializeComponent();
            this.subMaterialTypeId = dTO.subMaterialTypeId;
            this.subMaterialType = dTO.subMaterialType;
            this.textBox辅料编码.Text = dTO.subMaterialCode;
            this.textBox供应商.Text = dTO.subMaterialSupplier;
            this.textBox图片.Text = dTO.subMaterialPicture;
            this.textBox品番.Text = dTO.subMaterialNumber;
            this.textBox辅料名称.Text = dTO.subMaterialName;
            this.textBox规格.Text = dTO.subMaterialSpecDesc;
            try
            {
                this.pictureEdit图片.Image =
                    FileService.GetImageByUrl(
                        URL.图片url + URL.辅料图片路径 + dTO.subMaterialPicture
                        );
            }
            catch
            {
                this.pictureEdit图片.Image  = FunctionHelper.getSSHIRT();
            }
            this.is更新 = true;
        }

        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrWhiteSpace(this.textBox辅料编码.Text))
            {
                this.dTO辅料一览.subMaterialCode = this.textBox辅料编码.Text.Trim();
                this.dTO辅料一览.subMaterialPicture = this.textBox图片.Text.Trim();
            }
            this.dTO辅料一览.subMaterialTypeId = this.subMaterialTypeId;
            this.dTO辅料一览.subMaterialType = this.subMaterialType;
            this.dTO辅料一览.subMaterialSupplier = this.textBox供应商.Text.Trim();
            this.dTO辅料一览.subMaterialNumber = this.textBox品番.Text.Trim();
            this.dTO辅料一览.subMaterialName = this.textBox辅料名称.Text.Trim();
            this.dTO辅料一览.subMaterialSpecDesc = this.textBox规格.Text.Trim();
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "gif,jpg,png|*.gif;*.jpg;*.png;*.jpeg";
                ofd.Title = "请选择图片文件";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    String subMaterialCode = 辅料Service.post辅料保存(this.dTO辅料一览, ofd);
                    //if (!is更新)
                    //{
                    //    try
                    //    {
                    //        if (!String.IsNullOrEmpty(subMaterialCode))
                    //        {
                    //            DTO辅料一览 dto = 辅料Service.getAccessoryByAccessoryCode(subMaterialCode);
                    //            辅料Service.金蝶辅料保存("VEN00004", dto);
                    //        }
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        Console.WriteLine(ex.Message);
                    //        MessageBox.Show("金蝶未保存");
                    //    }
                    //}
                    MessageBox.Show("操作完成");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("操作失败");
            }
            this.Close();
        }
    }
}